define(
	[
		'require'
	],
	function(require) {
		var Controller = Ember.Controller.extend({
			needs: ["businessDetail", "businessCenter"],

			business: Ember.computed.alias('controllers.businessDetail.model'),

			pinTimePoint: Ember.computed.alias('controllers.businessDetail.pinTimePoint'),

			clickTimePoint: Ember.computed.oneWay('controllers.businessDetail.clickTimePoint'),

			initTime: function(){
		        var ctrl = this;
		        var startDate = ctrl.get("startDate");
		        var endDate = ctrl.get("endDate");
		        if(this.get("clickTimePoint") != null ){
		        	var date = new Date(this.get("clickTimePoint"));
		        	ctrl.set("endDate",new Date(+date+60000));
		        	if((isNaN(startDate))){
		        		ctrl.set("startDate",new Date(+date+60000)-60000);
		        	}else{
		        		ctrl.set("startDate",new Date(+date+60000)-(Math.abs(+endDate-(+startDate))));
		        	}
		        	
		        	
		        	setTimeout(function(){
		        		var startDate = APM.util.formatDate(new Date(+ctrl.get("startDate")), "yyyy/MM/dd H:mm");
		        		var endDate = APM.util.formatDate(new Date(+ctrl.get("endDate")), "yyyy/MM/dd H:mm");
		        		$('#config-demo').val(startDate+' - '+endDate);
		        	});
		        	
		        }else{
		        	var sdate = new Date(new Date() - timeD_value - 60000-60000);
			        sdate.setSeconds(0);
			        sdate.setMilliseconds(0); 
			        ctrl.set("startDate",new Date(+sdate));
			        var edate = new Date(new Date() - timeD_value-60000);
			        edate.setSeconds(0);
			        edate.setMilliseconds(0); 
			        ctrl.set("endDate",new Date(+edate));
		        }
		    },

			gridColumns: [{
					headerText: "IP地址",
					dataKey: "ip_port",
					cellFormatter: function(args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							args.$container.addClass("btn-link");
						}
					}
				}, {
					headerText: overviewOrCenterSwitch == "business" ? "交易量" : "吞吐量",
					dataKey: overviewOrCenterSwitch == "business" ? "performance.business/throughput/total" : "performance.system/throughput/total"
				}, {
					headerText: "平均响应时间ms",
					dataKey: overviewOrCenterSwitch == "business" ? "performance.business/response_time/avg" : "performance.system/response_time/avg",
					_fmtType: "transform_time_ms",
					cellFormatter: APM.util.indicatorCellFormatter
				},
				// {
				// 	headerText: "流量MB",
				// 	dataKey:"performance.network/bytes/total",
				// 	cellFormatter: function (args, colume) {
				// 		if (args.row.type & $.wijmo.wijgrid.rowType.data) {
				// 			args.$container.append(APM.util.flowFormatter(args.row.data.performance["network/bytes/total"]));
				// 			return true;
				// 		}
				// 	}
				// },
				{
					headerText: "成功率%",
					dataKey: overviewOrCenterSwitch == "business" ? "performance.business/success/rate" : "performance.system/success/rate",
					cellFormatter: function(args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var rate = overviewOrCenterSwitch == "business" ? "business/success/rate" : "system/success/rate";
							var ctrl = this.get("controller");
							var data = args.row.data.performance[rate];

							args.$container.text(data);
							return true;
						}
					}
				}, {
					headerText: "错误数",
					dataKey: overviewOrCenterSwitch == "business" ? "performance.business/error/count" : "performance.system/error/count"
				}
			],

			gridData: Ember.computed(function(key, value) {
				if (arguments.length == 1) {
					return Ember.A();
				} else {
					var allNodes = this.get("business.topoNodes");
					var allNodeMap = {};
					var result = Ember.A();
					allNodes.forEach(function(node) {
						allNodeMap[node.id] = node;
					});
					value.forEach(function(item) {
						var node = allNodeMap[item.serviceId];
						result.push({
							id: node.id,
							ip_port: node.ip + ":" + node.port,
							node: node,
							performance: item
						});
					});
					return result;
				}
			}),

			requireData: function() {
				var ctrl = this,
					appId = this.get("business.id"),
					node = this.get("model"),
					nodeIds = [];

				if (!(node.nodeType == 2 && node.nodeIds)) {
					return;
				}

				var params = {
					appId: appId,
					targetIds: node.nodeIds.join(),
					type: "service",
					startDate: +this.get("startDate"),
					endDate: +this.get("endDate"),
					"orderBy": this.get("orderBy"),
					"desc": this.get("desc"),
					"panelId": overviewOrCenterSwitch,
					"pageIndex": this.get("pageIndex"),
					"pageSize": this.get("pageSize")
				};

				//开发调试模式参数
				if (isLocalDebugModel) {
					params = undefined;
				}

				return this.store.ajax("GET", "performances/subtotal", params)
					.then(function(response) {
						var result = response.result;

						if (result.length == 0) {
							ctrl.set("pageIndex", 0);
							ctrl.set("pageSize", 10);
							ctrl.set("totalRows", 0); //设置为0显示没有数据
						} else {
							ctrl.set("pageIndex", response.meta.pageIndex);
							ctrl.set("pageSize", response.meta.pageSize);
							ctrl.set("totalRows", response.meta.total);
						}
						ctrl.set("gridData", Ember.A(response.result));
						return response;
					});
			},
			actions: {
				timePointChange: function(timeRange) {
					this.set("startDate", timeRange.startDate);
					this.set("endDate", timeRange.endDate);

					this.requireData();
				},
				timeRangesChange: function(timeRange) {
					var ctrl = this;
					var pinTimePoint = ctrl.get("controllers.businessDetail").pinTimePoint;

					ctrl.set("startDate", timeRange.startDate);
					ctrl.set("endDate", timeRange.endDate);


					var view = Broada.getView("businessTimeRangesTopo");
					if (ctrl.get("pinTimePoint") != timeRange.startDate) {
						ctrl.set("pinTimePoint", null);
					}

					ctrl.set("pageIndex", 0);
					ctrl.requireData();
				},

				gridSorted: function(e, data, view) {
					view.ensureControl(false);
					var dataKey = data.column.dataKey;

					var dataKey = dataKey.substring(dataKey.indexOf("."));
					this.set("orderBy", dataKey);
					this.set("desc", data.sortDirection == "descending" || undefined);
					this.requireData();
				},
				pageIndexChanged: function(e, data, view) {
					var pageNo = data.newPageIndex;
					this.set("pageIndex", pageNo);
					this.requireData();
				},

				gridCellClicked: function(e, args, view) {
					var ctrl = this;
					row = args.cell.row();
					column = args.cell.column();
					if (row.type & $.wijmo.wijgrid.rowType.data) {
						if (column.dataKey == "ip_port") {
							this.transitionToRoute("business.detail.nodedetail.overview", this.get("business"), row.data.node);
						}
					}
				}
			}
		});

		APM.BusinessDetailNodedetailClusterController = Controller;

		return Controller;
	}
);